package com.iplant.pdm.controller.fpc;

import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.pdm.service.FPCService;
import com.iplant.pdm.service.po.bop.FPCRoutePartPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/FPCRoutePartPoint")
public class FPCRoutePartPointController extends BasicController<FPCRoutePartPoint> {
    private static final Logger logger = LoggerFactory.getLogger(FPCRoutePartPointController.class);

    public FPCRoutePartPointController() {
        super(FPCRoutePartPoint.class);
    }

    @Autowired
    FPCService mFPCService;

    /**
     * 查询待竣工确认过滤项
     */
    @GetMapping("/ProductCurrent")
    public Object ProductCurrent(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);
            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"), -1);
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"), -1);
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"), -1);
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"), -1);


            Pagination wPagination = GetPagination(request);

            ServiceResult<List<FPCRoutePartPoint>> wServiceResult = mFPCService.FPC_QueryProductRoutePartPoint(wLoginUser, wFactoryID, wLineID, wCustomerID, wProductID, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    /**
     * 激活或禁用
     */
    @Override
    @PostMapping("/Active")
    public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            if (!wParam.containsKey("Active") || !wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            int wActive = StringUtils.parseInt(wParam.get("Active"));
            List<FPCRoutePartPoint> wFPCRoutePartPointList = CloneTool.CloneArray(wParam.get("data"),
                    FPCRoutePartPoint.class);
            if (wFPCRoutePartPointList == null || wFPCRoutePartPointList.size() == 0 || wFPCRoutePartPointList.stream().anyMatch(p -> p.ID <= 0)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mFPCService.FPC_RoutePartPointActive(wLoginUser, wActive,
                    wFPCRoutePartPointList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
